/*
 * Copyright 2017 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.zhongan.gateway.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhongan.gateway.common.enums.ResponseCodeEnum;
import com.zhongan.gateway.common.exception.GatewayException;
import com.zhongan.gateway.service.CacheService;
import com.zhongan.gateway.service.RedisService;
import com.zhongan.gateway.web.common.BaseController;
import com.zhongan.gateway.web.common.ResultBase;

/**
 * 类IndexController.java的实现描述：Index请求
 * 
 * @author wujian 2017年4月5日 下午2:45:36
 */
@Controller
@RestController
@RequestMapping(value = "/cache")
public class CacheController extends BaseController {
    //    private static final Logger LOG      = LoggerFactory.getLogger(CacheController.class);

    @Resource
    private CacheService        cacheService;

    @Resource
    private RedisService        redisService;

    private final static String PASSWORD = "81dc9bdb52d04dc20036dbd8313ed055";

    @RequestMapping(value = "/api")
    public ResultBase<Object> api(String pwd) throws GatewayException {
        checkPwd(pwd);
        cacheService.clearApi();

        return getSucResultBase("success");
    }

    void checkPwd(String pwd) throws GatewayException {
        if (!PASSWORD.equals(pwd)) {
            throw new GatewayException(ResponseCodeEnum.FAIL.getCode(), "密码错误");
        }
    }

    @RequestMapping("/openFused")
    ResultBase<Object> openFused(String pwd, String key) throws GatewayException {
        checkPwd(pwd);
        redisService.openFused(key);
        return getSucResultBase("success");
    }

    @RequestMapping("/apiPrivilege")
    ResultBase<Object> apiPrivilege(String pwd) throws GatewayException {
        checkPwd(pwd);
        cacheService.clearApiPrivilege();
        return getSucResultBase("success");
    }

    @RequestMapping("/apiAttribute")
    ResultBase<Object> apiAttribute(String pwd) throws GatewayException {
        checkPwd(pwd);
        cacheService.clearApiAttribute();
        return getSucResultBase("success");
    }

    @RequestMapping("/developer")
    ResultBase<Object> developer(String pwd) throws GatewayException {
        checkPwd(pwd);
        cacheService.clearDeveloper();
        return getSucResultBase("success");
    }

    @RequestMapping("/developerIp")
    ResultBase<Object> developerIp(String pwd) throws GatewayException {
        checkPwd(pwd);
        cacheService.clearDeveloperIp();
        return getSucResultBase("success");
    }

}
